﻿using System.Collections.Generic;
using System.Linq;
using Harvest.Api.Model;
using Harvest.Api.Model.Internal;
using Harvest.Api.Types;

namespace Harvest.Api.ExtensionMethods
{

    public static class ClientExtensions
    {

        public static Client ToClient(this ClientItem entity)
        {
            var result = new Client
            {
                Active = entity.Active,
                CacheVersion = entity.CacheVersion,
                CreatedAt = entity.CreatedAt,
                Currency = entity.Currency,
                CurrencySymbol = entity.CurrencySymbol,
                DefaultInvoiceTimeframe = entity.DefaultInvoiceTimeframe,
                Details = entity.Details.Replace("\n", "\r\n"),
                HighriseId = entity.HighriseId.HasValue ? entity.HighriseId.Value : (int?)null,
                Id = entity.Id.HasValue ? entity.Id.Value : (int?)null,
                Name = entity.Name,
                UpdatedAt = entity.UpdatedAt
            };
            return result;
        }


        public static ClientItem ToClientItem(this Client entity)
        {
            var highriseId = new NullableInt();
            if (entity.HighriseId.HasValue) highriseId = entity.HighriseId.Value;

            var id = new NullableInt();
            if (entity.Id.HasValue) id = entity.Id.Value;

            var result = new ClientItem
                             {
                Active = entity.Active,
                CacheVersion = entity.CacheVersion,
                CreatedAt = entity.CreatedAt,
                Currency = entity.Currency,
                CurrencySymbol = entity.CurrencySymbol,
                DefaultInvoiceTimeframe = entity.DefaultInvoiceTimeframe,
                Details = entity.Details.Replace("\r\n", "\n"),
                HighriseId = highriseId,
                Id = id,
                Name = entity.Name,
                UpdatedAt = entity.UpdatedAt
            };
            return result;
        }

        
        public static List<Client> ToClients(this IEnumerable<ClientItem> entity)
        {
            var result = entity.Select(x => x.ToClient()).ToList();
            return result;
        }


        public static List<ClientItem> ToClientsItem(this IEnumerable<Client> entity)
        {
            var result = entity.Select(x => x.ToClientItem()).ToList();
            return result;
        }
    }

}
